// pages/bookexe/bookexe.js
var businessLogic = require('../../../utils/NetWork/Model/BookbusinessLogic.js')
var common = require('../../../utils/common.js')
var dataUrl = require('../../../utils/NetWork/sysJson.js')
// canvas 全局配置
var context = null;// 使用 wx.createContext 获取绘图上下文 context
var isButtonDown = false;
var arrx = [];
var arry = [];
var arrz = [];
var canvasw = 0;
var canvash = 0;

// var strokeCoordinate = [];		//每个笔画的点阵数组
var SCALE = 0.11;				//缩小比例，0.25即缩小四分之一
var STORECOLOR = "red";		//设置颜色
var WRITESPEED = 1000;				//写字速度，值越大，写字越慢
var LINEWIDTH = 1;			//点阵宽度(一般取值<=1)
var EVERYPRINT = 20;			//每次画出点的个数的两倍（如每次画10个点阵点，则该值为20）
var initNum = 2;				//固定值
var itemNum = EVERYPRINT;


var resource = new Array();
var pageMapResource = new Array();
var height = "";
var width = "";
Page({

  canvasIdErrorCallback: function (e) {
    console.error(e.detail.errMsg)
  },
  canvasStart: function (event) {
    isButtonDown = true;
    arrz.push(0);
    arrx.push(event.changedTouches[0].x);
    arry.push(event.changedTouches[0].y);
    //context.moveTo(event.changedTouches[0].x, event.changedTouches[0].y);

  },
  canvasMove: function (event) {
    if (isButtonDown) {
      arrz.push(1);
      arrx.push(event.changedTouches[0].x);
      arry.push(event.changedTouches[0].y);

    };

    for (var i = 0; i < arrx.length; i++) {
      if (arrz[i] == 0) {
        context.moveTo(arrx[i], arry[i])
      } else {
        context.lineTo(arrx[i], arry[i])
      };

    };
    context.clearRect(0, 0, canvasw, canvash);
    context.stroke();

    context.draw(true);
  },
  canvasEnd: function (event) {
    isButtonDown = false;
  },

  getimg: function () {
    //
    //
    if (arrx.length == 0) {
      wx.showModal({
        title: '提示',
        content: '签名内容不能为空！',
        showCancel: false
      });
      return false;
    };
    //生成图片
    wx.canvasToTempFilePath({
      canvasId: 'canvas',
      success: function (res) {
        // console.log(res.tempFilePath);
        //存入服务器
        wx.uploadFile({
          url: 'a.php', //接口地址
          filePath: res.tempFilePath,
          name: 'file',
          formData: {                 //HTTP 请求中其他额外的 form data 
            'user': 'test'
          },
          success: function (res) {
            console.log(res);
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {

          }
        });
      }
    })

  },
  /**
   * 页面的初始数据
   */
  data: {
    //src:"http://sc1.111ttt.com/2016/1/09/28/202280605509.mp3",
    item: [],
    page: [],
    bookPage: [],
    unitItem: [],
    lessonItem: [],
    datatype: 'json',
    async: true,
    xiexie: "",
    xiexie2: "",
    curNavId: "",
    open: false,
    mark: 0,
    newmark: 0,
    istoright: true,
    content: '',
    ischeck: false,

    flag: true,
    post_title: "",
    post: "",
    strokeCoordinate: "",
    title: "看一看",

    k_tag_hw: "12%",
    k_tag_hh: "25%",
    k_tsdx_ht: "31%",
    k_tsdx_hl: "32%",
    k_tsdx_hf: "30rpx",

    t_tag_hw: "10%",
    t_tag_hh: "20%",
    t_tsdx_ht: "31%",
    t_tsdx_hl: "32%",
    t_tsdx_hf: "30rpx",

    d_tag_hw: "10%",
    d_tag_hh: "20%",
    d_tsdx_ht: "31%",
    d_tsdx_hl: "32%",
    d_tsdx_hf: "30rpx",

    x_tag_hw: "10%",
    x_tag_hh: "20%",
    x_tsdx_ht: "31%",
    x_tsdx_hl: "32%",
    x_tsdx_hf: "30rpx",

    kanlist: "block",
    tinglist: "none",
    dulist: "none",
    xielist: "none",

    //fg: 0,
  },
  //
  tap_ch: function (e) {
    //
    if (this.data.open) {
      this.setData({
        open: false
      });
    } else {
      this.setData({
        open: true
      });
      if (!this.data.unitItem[0]) {
        this.unitFind();
      }
    }
  },
  tap_start: function (e) {
    // touchstart事件
    this.data.mark = this.data.newmark = e.touches[0].pageX;
  },
  tap_drag: function (e) {
    // touchmove事件

    /*
    * 手指从左向右移动
    * @newmark是指移动的最新点的x轴坐标 ， @mark是指原点x轴坐标
    */
    this.data.newmark = e.touches[0].pageX;
    if (this.data.mark < this.data.newmark) {
      this.istoright = true;
    }
    /*
    * 手指从右向左移动
    * @newmark是指移动的最新点的x轴坐标 ， @mark是指原点x轴坐标
    */
    if (this.data.mark > this.data.newmark) {
      this.istoright = false;
    }
    this.data.mark = this.data.newmark;
  },
  tap_end: function (e) {
    // touchend事件
    this.data.mark = 0;
    this.data.newmark = 0;
    if (this.istoright) {
      this.setData({
        open: true
      });
    } else {
      this.setData({
        open: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var tempId = options.id;
    var c = common.Getorgid();
    this.setData({
      // post_title: post_content,
      orgid: c.orgid,
      userid: c.userid,
      bookid: common.getStorageSync("bookid"),
      index: 0


    });

    // let ss =businessLogic.findNewsList();
    // 使用 wx.createContext 获取绘图上下文 context
    this.mexie();
    this.firstIn(tempId);
    var that = this

    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 可使用窗口宽度、高度
        height = res.windowHeight
        width = res.windowWidth
        // 计算主体部分高度,单位为px
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    this.audioCtx = wx.createAudioContext('myAudio')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },



  mexie: function () {
    context = wx.createCanvasContext('canvas');
    context.beginPath()
    context.setStrokeStyle('#000000');
    context.setLineWidth();
    context.setLineCap('round');
    context.setLineJoin('round');
  },
  ti: function (content) {
    if (!this.data.ischeck) {
      this.setData({
        strokeCoordinate: [],
      })
    }
    this.setData({
      ischeck: !this.data.ischeck
    })
    this.computerxie();
  },
  cleardraw: function () {
    //清除画布
    console.log(234)
    context = wx.createCanvasContext('canvas');
    arrx = [];
    arry = [];
    arrz = [];
    context.draw()


  },

  //4.3.2.5 查询写的文字信息（已测试）
  xiemap: function () {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "title", "searchValue": "" + this.data.xiexie + "" }
    businessLogic.QueryText(params, (res) => {
      console.log(res)

      this.setData({
        post: res
      })
    });
  },

  //4.3.2.6
  computerxie: function () {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "title", "searchValue": "" + this.data.xiexie + "" }
    businessLogic.QueryCutting(params, (res) => {
      // console.log(data);
      console.log(res);
      var strokeW = 0;
      var strokeH = 0;
      var strokeX = 0;
      var strokeY = 0;
      var escapeCode = res[0].content;
      var deCode = JSON.parse(unescape(escapeCode));
      var directionArr = new Array();
      var widthNum = 0;
      var hightNum = 0;
      var widthNumArr = new Array();
      var heightNumArr = new Array();
      for (var i = 0; i < deCode.length; i++) {
        // chineseString = chineseString + deCode[i].Stroke +"";
        for (var j = 0; j < deCode[i].StrokeList.length; j++) {
          strokeW = parseInt(deCode[i].StrokeList[j].W);
          strokeH = parseInt(deCode[i].StrokeList[j].H);
          strokeX = parseInt(deCode[i].StrokeList[j].X);
          strokeY = parseInt(deCode[i].StrokeList[j].Y);
          this.data.strokeCoordinate.push(deCode[i].StrokeList[j].strokeCoordinate);
        }
      }
      const self = this
      self.strokeDirection(this.data.strokeCoordinate);
      this.setData({
        strokeCoordinate: "",
        //fg:1
        bihua: deCode,
      })
      console.log(deCode)
    });
  },

  strokeDirection: function (sArr) {
    // 使用 wx.createContext 获取绘图上下文 context
    var j = 0;
    var i = 0;
    var contextxie = wx.createCanvasContext('firstCanvas')
    contextxie.setLineWidth(LINEWIDTH);

    contextxie.beginPath();

    var pwInterval = setInterval(function () {
      contextxie.setStrokeStyle(STORECOLOR);
      contextxie.moveTo(sArr[i][j] * SCALE, sArr[i][j + 1] * SCALE);
      contextxie.lineTo(sArr[i][j + 2] * SCALE, sArr[i][j + 3] * SCALE);
      contextxie.stroke();
      contextxie.draw(true);
      j = j + 4;
      if (j >= sArr[i].length) {
        i = i + 1;
        j = 0;
        if (i == sArr.length) {
          clearInterval(pwInterval);
        }

      }
    }, 10)

  },
  hide: function () {
    this.cleardraw();
    this.setData({
      flag: true,
    })
  },
  show: function (e) {
    this.setData({
      bihua: "",
      flag: false,
      resourceTitle: '',//清空
      resourceContent: ''//清空
    });
    pageMapResource.splice(0, pageMapResource.length)//清空map上的事件
    this.QueryMap(e);

  },
  // 4.3.2.3 查询map的事件列表
  QueryMap: function (e) {
    //取map上存的事件
    var c = common.Getorgid();
    var mapid = e.currentTarget.dataset.mapid;
    console.log("11122" + mapid)
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "mapid", "searchValue": mapid }
    businessLogic.QueryMap(params, (res) => {

      for (let a = 0; a < res.length; a++) {
        if (res[a].type == 3) {
          this.setData({
            xiexie: res[a].resourceid
          });
          //console.log(res)
        }
      }
      for (let a = 0; a < res.length; a++) {
        if (res[a].type == 1) {
          this.setData({
            xiexie2: res[a].resourceid
          });
          console.log(res)
        }
      }

      var arrs = [];

      arrs = this.data.xiexie2.split('||');
      console.log("rid", arrs[1]);
      this.setData({
        rid: arrs[1]
      })
      let videoparams = { "fileid": this.data.rid }
      businessLogic.findFileInfo(videoparams, (res2) => {
        var uri = [];
        console.log("yin", res2)
        if (res2.filepath.indexOf("pub") > -1) {
          var aaaa = res2.filepath.replace(/\\/g, "/");
          var a = aaaa.replace(/%/g, '%25');
          console.log(a)
          uri = dataUrl.dataUrl + a.split("admin")[1];

        } else {
          uri = dataUrl.dataUrl + res2.filepath
        }
        this.setData({
          yinpin:uri
        })
        console.log("5555555",uri)
      })
      for (let a = 0; a < res.length; a++) {
        pageMapResource.push(res[a])
      }
      this.kan(e);
      this.xiemap();
      // this.computerxie();

    });
  },
  kan: function (e) {
    //看事件
    this.cleardraw();
    var haved
    var resourceTitle
    var resourceContent
    for (var i = 0; i < pageMapResource.length; i++) {
      switch (pageMapResource[i].type) {
        case "0":
          haved = 1;
          var str = pageMapResource[i].resourceid;
          var arr = str.split("||");
          resourceTitle = arr[0];
          resourceContent = arr[1];
          break;
      }
    }
    if (haved == 1) {
      this.setData({
        resourceTitle: resourceTitle,
        resourceContent: resourceContent,
        title: "看一看",
        //
        k_tag_hw: "12%",
        k_tag_hh: "25%",
        k_tsdx_ht: "31%",
        k_tsdx_hl: "27%",
        k_tsdx_hf: "40rpx",

        t_tag_hw: "10%",
        t_tag_hh: "20%",
        t_tsdx_ht: "31%",
        t_tsdx_hl: "32%",
        t_tsdx_hf: "30rpx",

        d_tag_hw: "10%",
        d_tag_hh: "20%",
        d_tsdx_ht: "31%",
        d_tsdx_hl: "32%",
        d_tsdx_hf: "30rpx",

        x_tag_hw: "10%",
        x_tag_hh: "20%",
        x_tsdx_ht: "31%",
        x_tsdx_hl: "32%",
        x_tsdx_hf: "30rpx",

        kanlist: "block",
        tinglist: "none",
        dulist: "none",
        xielist: "none"
        //
      })
    } else {
      wx.showModal({
        content: '(⊙o⊙)…额?!好像没有可看的',
        showCancel: false,
        confirmText: '知道了',
      })
      this.setData({
        resourceTitle: '',
        resourceContent: '',
        title: "看一看",
        //
        k_tag_hw: "12%",
        k_tag_hh: "25%",
        k_tsdx_ht: "31%",
        k_tsdx_hl: "27%",
        k_tsdx_hf: "40rpx",

        t_tag_hw: "10%",
        t_tag_hh: "20%",
        t_tsdx_ht: "31%",
        t_tsdx_hl: "32%",
        t_tsdx_hf: "30rpx",

        d_tag_hw: "10%",
        d_tag_hh: "20%",
        d_tsdx_ht: "31%",
        d_tsdx_hl: "32%",
        d_tsdx_hf: "30rpx",

        x_tag_hw: "10%",
        x_tag_hh: "20%",
        x_tsdx_ht: "31%",
        x_tsdx_hl: "32%",
        x_tsdx_hf: "30rpx",

        kanlist: "block",
        tinglist: "none",
        dulist: "none",
        xielist: "none"
        //
      })
    }
  },
  ting: function () {
    //听事件
    this.cleardraw();
    var haved
    var resourceTitle
    var resourceContent
    for (var i = 0; i < pageMapResource.length; ++i) {
      switch (pageMapResource[i].type) {
        case "1":
          haved = 1;
          var str = pageMapResource[i].resourceid;
          var arr = str.split("||");
          resourceTitle = arr[0];//文字
          resourceContent = arr[1];//音频id
          break;
      }
    }
    if (haved == 1) {
      this.setData({
        resourceTitle: resourceTitle,
        resourceContent: resourceContent,

        title: "听一听",

        k_tag_hw: "10%",
        k_tag_hh: "20%",
        k_tsdx_ht: "31%",
        k_tsdx_hl: "32%",
        k_tsdx_hf: "30rpx",

        t_tag_hw: "12%",
        t_tag_hh: "25%",
        t_tsdx_ht: "31%",
        t_tsdx_hl: "27%",
        t_tsdx_hf: "40rpx",

        d_tag_hw: "10%",
        d_tag_hh: "20%",
        d_tsdx_ht: "31%",
        d_tsdx_hl: "32%",
        d_tsdx_hf: "30rpx",

        x_tag_hw: "10%",
        x_tag_hh: "20%",
        x_tsdx_ht: "31%",
        x_tsdx_hl: "32%",
        x_tsdx_hf: "30rpx",

        kanlist: "none",
        tinglist: "block",
        dulist: "none",
        xielist: "none",
      })
    } else {
      wx.showModal({
        content: '(⊙o⊙)…额?!好像没有可听的',
        showCancel: false,
        confirmText: '知道了',
      })
      this.setData({
        resourceTitle: '',
        resourceContent: '',
        title: "听一听",

        k_tag_hw: "10%",
        k_tag_hh: "20%",
        k_tsdx_ht: "31%",
        k_tsdx_hl: "32%",
        k_tsdx_hf: "30rpx",

        t_tag_hw: "12%",
        t_tag_hh: "25%",
        t_tsdx_ht: "31%",
        t_tsdx_hl: "27%",
        t_tsdx_hf: "40rpx",

        d_tag_hw: "10%",
        d_tag_hh: "20%",
        d_tsdx_ht: "31%",
        d_tsdx_hl: "32%",
        d_tsdx_hf: "30rpx",

        x_tag_hw: "10%",
        x_tag_hh: "20%",
        x_tsdx_ht: "31%",
        x_tsdx_hl: "32%",
        x_tsdx_hf: "30rpx",

        kanlist: "none",
        tinglist: "block",
        dulist: "none",
        xielist: "none",
        //
      })
    }
  },
  du: function () {
    //读事件
    this.cleardraw();
    var haved
    var resourceTitle
    //var resourceContent
    for (var i = 0; i < pageMapResource.length; ++i) {
      switch (pageMapResource[i].type) {
        case "2":
          haved = 1;
          var str = pageMapResource[i].resourceid;
          var arr = str.split("||");
          resourceTitle = arr[0];//文字
          // resourceContent = arr[1];//音频id
          break;
      }
    }
    if (haved == 1) {
      this.setData({
        resourceTitle: resourceTitle,
        //resourceContent: resourceContent,
        title: "读一读",

        k_tag_hw: "10%",
        k_tag_hh: "20%",
        k_tsdx_ht: "31%",
        k_tsdx_hl: "32%",
        k_tsdx_hf: "30rpx",

        t_tag_hw: "10%",
        t_tag_hh: "20%",
        t_tsdx_ht: "31%",
        t_tsdx_hl: "32%",
        t_tsdx_hf: "30rpx",

        d_tag_hw: "12%",
        d_tag_hh: "25%",
        d_tsdx_ht: "31%",
        d_tsdx_hl: "27%",
        d_tsdx_hf: "40rpx",

        x_tag_hw: "10%",
        x_tag_hh: "20%",
        x_tsdx_ht: "31%",
        x_tsdx_hl: "32%",
        x_tsdx_hf: "30rpx",

        kanlist: "none",
        tinglist: "none",
        dulist: "block",
        xielist: "none",
      })
    } else {
      wx.showModal({
        content: '(⊙o⊙)…额?!好像没有可读的',
        showCancel: false,
        confirmText: '知道了',
      })
      this.setData({
        resourceTitle: '',
        resourceContent: '',
        title: "读一读",

        k_tag_hw: "10%",
        k_tag_hh: "20%",
        k_tsdx_ht: "31%",
        k_tsdx_hl: "32%",
        k_tsdx_hf: "30rpx",

        t_tag_hw: "10%",
        t_tag_hh: "20%",
        t_tsdx_ht: "31%",
        t_tsdx_hl: "32%",
        t_tsdx_hf: "30rpx",

        d_tag_hw: "12%",
        d_tag_hh: "25%",
        d_tsdx_ht: "31%",
        d_tsdx_hl: "27%",
        d_tsdx_hf: "40rpx",

        x_tag_hw: "10%",
        x_tag_hh: "20%",
        x_tsdx_ht: "31%",
        x_tsdx_hl: "32%",
        x_tsdx_hf: "30rpx",

        kanlist: "none",
        tinglist: "none",
        dulist: "block",
        xielist: "none",
      })
    }
  },





  xie: function () {
    this.setData({
      strokeCoordinate: []
    })

    let cc = wx.createCanvasContext('firstCanvas');
    context = wx.createCanvasContext('canvas');
    cc.draw();
    context.draw();
    //写事件
    var haved
    var resourceTitle
    var resourceContent
    for (var i = 0; i < pageMapResource.length; ++i) {
      switch (pageMapResource[i].type) {
        case "3":
          haved = 1;
          var str = pageMapResource[i].resourceid;
          resourceTitle = str;//文字
          break;
      }
    }
    if (haved == 1) {
      this.setData({
        resourceTitle: resourceTitle,

        title: "写一写",

        k_tag_hw: "10%",
        k_tag_hh: "20%",
        k_tsdx_ht: "31%",
        k_tsdx_hl: "32%",
        k_tsdx_hf: "30rpx",

        t_tag_hw: "10%",
        t_tag_hh: "20%",
        t_tsdx_ht: "31%",
        t_tsdx_hl: "32%",
        t_tsdx_hf: "30rpx",

        d_tag_hw: "10%",
        d_tag_hh: "20%",
        d_tsdx_ht: "31%",
        d_tsdx_hl: "32%",
        d_tsdx_hf: "30rpx",

        x_tag_hw: "12%",
        x_tag_hh: "25%",
        x_tsdx_ht: "31%",
        x_tsdx_hl: "27%",
        x_tsdx_hf: "40rpx",

        kanlist: "none",
        tinglist: "none",
        dulist: "none",
        xielist: "block"
      })
    } else {
      this.setData({
        resourceTitle: '',
        resourceContent: '',
        title: "写一写",

        k_tag_hw: "10%",
        k_tag_hh: "20%",
        k_tsdx_ht: "31%",
        k_tsdx_hl: "32%",
        k_tsdx_hf: "30rpx",

        t_tag_hw: "10%",
        t_tag_hh: "20%",
        t_tsdx_ht: "31%",
        t_tsdx_hl: "32%",
        t_tsdx_hf: "30rpx",

        d_tag_hw: "10%",
        d_tag_hh: "20%",
        d_tsdx_ht: "31%",
        d_tsdx_hl: "32%",
        d_tsdx_hf: "30rpx",

        x_tag_hw: "12%",
        x_tag_hh: "25%",
        x_tsdx_ht: "31%",
        x_tsdx_hl: "27%",
        x_tsdx_hf: "40rpx",

        kanlist: "none",
        tinglist: "none",
        dulist: "none",
        xielist: "block"
      })
    }
  },
  // 4.3.2.4 查询该课下所有的书页（已测试
  firstIn: function (tempId) {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "bookid", "searchValue": tempId }
    businessLogic.AllQueryMap(params, (res) => {
      console.log(res)
      this.setData({
        page: res
      })
      //是否要判断有无书页？
      this.pageDetail();
    });
  },
  // 4.3.2.1 查询书页内容（已测试）
  pageDetail: function () {
    if (this.data.page.length == 0) {

    } else {
      let params = { "orgid": this.data.orgid, "userid": this.data.userid, "id": this.data.page[0].id }
      businessLogic.QueryPage(params, (res) => {
        this.setData({
          item: res
        });

      });
      this.firstQueryPageMap();
    }

  },
  // 4.3.2.2 查询书页map列表（已测试）
  firstQueryPageMap: function (e) {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "bookpageid", "searchValue": this.data.page[0].id }
    businessLogic.QueryPageMap(params, (res) => {
      for (let a = 0; a < res.length; a++) {
        var w = parseInt(res[a].w);
        var h = parseInt(res[a].h);
        var x = parseInt(res[a].x);
        var y = parseInt(res[a].y);


        var wWindow = (width / 400) * w;
        var hWindow = (height / 600) * h;
        var xWindow = (height / 600) * x;
        var yWindow = (width / 400) * y;
        res[a].wWindow = []
        res[a].wWindow.push(wWindow);
        res[a].hWindow = []
        res[a].hWindow.push(hWindow);
        res[a].xWindow = []
        res[a].xWindow.push(xWindow);
        res[a].yWindow = []
        res[a].yWindow.push(yWindow);
      }
      this.setData({
        pageMap: res
      });
    });
  },
  // 4.3.2.2 查询书页map列表（已测试）
  beforePageMap: function (e) {
    var title = e.currentTarget.dataset.title;
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "bookpageid", "searchValue": this.data.page[title - 2].id }
    businessLogic.QueryPageMap(params, (res) => {
      for (let a = 0; a < res.length; a++) {
        var w = parseInt(res[a].w);
        var h = parseInt(res[a].h);
        var x = parseInt(res[a].x);
        var y = parseInt(res[a].y);

        var wWindow = (width / 400) * w;
        var hWindow = (height / 600) * h;
        var xWindow = (height / 600) * x;
        var yWindow = (width / 400) * y;

        res[a].wWindow = []
        res[a].wWindow.push(wWindow);
        res[a].hWindow = []
        res[a].hWindow.push(hWindow);
        res[a].xWindow = []
        res[a].xWindow.push(xWindow);
        res[a].yWindow = []
        res[a].yWindow.push(yWindow);
      }
      this.setData({
        pageMap: res
      })
    });
  },
  // 4.3.2.2 查询书页map列表（已测试）
  afterPageMap: function (e) {
    var title = e.currentTarget.dataset.title;
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "bookpageid", "searchValue": this.data.page[title].id }
    businessLogic.QueryPageMap(params, (res) => {
      for (let a = 0; a < res.length; a++) {
        var w = parseInt(res[a].w);
        var h = parseInt(res[a].h);
        var x = parseInt(res[a].x);
        var y = parseInt(res[a].y);

        var wWindow = (width / 400) * w;
        var hWindow = (height / 600) * h;
        var xWindow = (height / 600) * x;
        var yWindow = (width / 400) * y;

        res[a].wWindow = []
        res[a].wWindow.push(wWindow);
        res[a].hWindow = []
        res[a].hWindow.push(hWindow);
        res[a].xWindow = []
        res[a].xWindow.push(xWindow);
        res[a].yWindow = []
        res[a].yWindow.push(yWindow);
      }
      this.setData({
        pageMap: res
      })
    });
  },
  // 4.3.2.1 查询书页内容（已测试）
  beforePage: function (e) {
    var title = e.currentTarget.dataset.title;
    if (title == 1) {
      wx.showToast({
        title: '没有上一页了吧？！',
        icon: 'none',
        duration: 500
      })
    } else {
      var params = { "orgid": this.data.orgid, "userid": this.data.userid, "id": this.data.page[title - 2].id }
      businessLogic.QueryPage(params, (res) => {

        this.setData({
          item: res
        });
        this.beforePageMap(e);
      });
    }
  },
  afterPage: function (e) {
    var title = e.currentTarget.dataset.title;
    var pageall = e.currentTarget.dataset.pageall;
    if (title == pageall) {
      wx.showToast({
        title: '完啦',
        icon: 'none',
        duration: 500
      })
    } else {
      var params = { "orgid": this.data.orgid, "userid": this.data.userid, "id": this.data.page[title].id }
      businessLogic.QueryPage(params, (res) => {
        this.setData({
          item: res
        })
        this.afterPageMap(e);
      })
    }
  },
  // 4.3.1 查询书本单元目录（已测试）
  unitFind: function (e) {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "bookid", "searchValue": this.data.bookid }
    businessLogic.bookFindFrontInfoTempList(params, (res) => {
      this.setData({
        unitItem: res
      });

      //this.lessonid();
    });
  },
  // 4.3.1.1 查询单元下的课目录（已测试）
  lessonid: function (e) {
    var params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "bookid,unitid", "searchValue": this.data.unitItem[0].bookid + "," + this.data.unitItem[0].id }
    businessLogic.QueryUnit(params, (res) => {
      //jc
      //console.log(index);

      var index = []
      index = parseInt(e.currentTarget.dataset.index)

      var that = this;
      that.setData({
        lessonItem: res,
        curNavId: index,
      })

    });
  },
  //播放
  audioPlay: function () {
   this.audioCtx.play()
   console.log(this.audioCtx.play)
 
 },
 
 audioPause: function () {
    this.audioCtx.pause() 
 },

 audioStart: function () {
    this.audioCtx.seek(0) 
 },
  //录音
  Startrecord: function () {
    common.startRecord();
  },
  Stoprecord: function () {
    common.stopRecord();
  },
  Playrecord: function () {
    common.playRecord();
  },







})